package com.nannybear.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.widget.ProgressBar;

import com.nannybear.Model.ProgressItem;
import com.nannybear.R;

import java.util.ArrayList;

/**
 * \brief The timeline
 * \author Audrey LOISEAU
 * \date 27-04-2015
 */

/********************************* COPYRIGHT *********************************
 * Copyright (c) 2015, Audrey LOISEAU and Benjamin LANCINOT.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *************************************************************************************/

public class TimelineView extends ProgressBar {
    /********** DECLARATIONS **********/
    // Variables
    private ArrayList<ProgressItem> mProgressItemsList;
    private Paint progressPaint;
    private RectF progressRect;
    static private final double NOISE = 2.0;
    static private final double QUIET = 1.0;
    static private final double OFFLINE = -1.0;

    // Handler
    private final Handler myHandler = new Handler();

    /************ METHODS ************/
    /**
     * \brief Constructor
     * \param context   The context
     */
    public TimelineView(Context context) {
        super(context);
        init();
    }

    /**
     * \brief Constructor
     * \param context   The context
     * \param attrs     Collection of attributes of XML
     */
    public TimelineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * \brief Constructor
     * \param context   The context
     * \param attrs     Collection of attributes of XML
     * \param defStyle  Style of the timeline
     */
    public TimelineView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    /**
     * \brief Initialization
     */
    private void init(){
        mProgressItemsList = new ArrayList<>();
        progressPaint = new Paint();
        progressRect = new RectF();
    }

    /**
     * \brief Invalidate the view
     * \param mProgressItemsList    The list of progress item
     */
    public void redraws(ArrayList<ProgressItem> mProgressItemsList){
        setmProgressItemsList(mProgressItemsList);
        myHandler.post(new Runnable() {
            @Override
            public void run() {
                invalidate();
            }
        });
    }


    /**
     * \brief Draw the timeline
     * \param canvas    The canvas on which the background will be drawn
     */
    protected void onDraw(@NonNull Canvas canvas) {
        if (mProgressItemsList.size() > 0) {
            int progressBarWidth = getWidth();
            int progressBarHeight = getHeight();
            int lastProgressX = 0;
            int progressItemWidth, progressItemRight;

            for (int i = 0; i < mProgressItemsList.size(); i++) {
                ProgressItem progressItem = mProgressItemsList.get(i);
                if (progressItem.state == NOISE) {
                    progressPaint.setColor(getResources().getColor(R.color.red));
                }
                else if (progressItem.state == QUIET) {
                    progressPaint.setColor(getResources().getColor(R.color.green));
                }
                else if(progressItem.state == OFFLINE){
                    progressPaint.setColor(getResources().getColor(R.color.grey_value));
                }
                else{
                    progressPaint.setColor(getResources().getColor(R.color.orange));
                }
                progressItemWidth = (int) (progressItem.percentage * progressBarWidth / 100);
                progressItemRight = lastProgressX + progressItemWidth;

                // for last item give right to progress item to the width
                if ((i == mProgressItemsList.size() - 1) && (progressItemRight != progressBarWidth)) {
                    progressItemRight = progressBarWidth;
                }

                progressRect.set(lastProgressX, 0, progressItemRight, progressBarHeight);
                canvas.drawRoundRect(progressRect, 8, 8, progressPaint);
                lastProgressX = progressItemRight;
            }
            super.onDraw(canvas);
        }
    }

    /*********** ACCESSORS ***********/
    /**
     * \brief Set method for the progress item list
     */
    public void setmProgressItemsList(ArrayList<ProgressItem> mProgressItemsList) {
        this.mProgressItemsList = mProgressItemsList;
    }
}
